package com.sunhao.blog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunhao.blog.entity.UserAuth;
import com.sunhao.blog.mapper.UserAuthMapper;
import com.sunhao.blog.model.dto.UserAuthDto;
import com.sunhao.blog.model.vo.ResultVO;
import com.sunhao.blog.service.UserAuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Objects;


@Service
public class UserAuthServiceImpl extends ServiceImpl<UserAuthMapper, UserAuth> implements UserAuthService {
    @Autowired
    private UserAuthMapper userAuthMapper;


    @Override
    public ResultVO login(UserAuthDto userAuthDto) {
        //判断对象是否为空
        if (Objects.isNull(userAuthDto)) {
            return ResultVO.fail("账号或密码不为空");
        }
        //判断账号是否存在，后面会校验，如果为空，查询不到
//        if (!StringUtils.hasText(userAuthDto.getUsername())) {
//            return ResultVO.fail("账号或密码不为空");
//        }

        //判断密码是否为空
        if (!StringUtils.hasText(userAuthDto.getPassword())) {
            return ResultVO.fail("账号或密码不为空");
        }

        //校验账户是否存在
        LambdaQueryWrapper<UserAuth> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserAuth::getUsername, userAuthDto.getUsername());
        UserAuth user = userAuthMapper.selectOne(lambdaQueryWrapper);
        //user为空抛空指针
        if (Objects.isNull(user)) {
            return ResultVO.fail("账号不存在");
        }
        if (!userAuthDto.getUsername().equals(user.getUsername())) {
            return ResultVO.fail("账号不存在");
        }


        // TODO: 2024/10/11 校验密码加密功能 
        //校验密码是否正确
        if (!user.getPassword().equalsIgnoreCase(userAuthDto.getPassword())) {
            return ResultVO.fail("密码不正确");
        }

        // TODO: 2024/10/11 返回token
        //登录成功，返回token
        return new ResultVO<>("token1111", "登录成功");


        // TODO: 2024/10/11 验证码功能未实现 

    }
}
